<template>
	<view class="page">
		<u-form :model="userData" ref="uForm">
			<view class="address-input">

				<view class="list-input">
					<u-form-item label="姓名" prop="name">
						<u-input v-model="userData.username" />
					</u-form-item>
				</view>


				<view class="list-input">
					<u-form-item label="电话" prop="phone">
						<u-input v-model="userData.phone" placeholder="请输入电话号码" />
					</u-form-item>
				</view>

				<view class="list-input">
					<u-form-item prop="values">
						<text class="title">收货地址</text>
						
							<u-input  v-model="userData.values"  @click="selectAddress" />
						<!-- <view class="content" >
							{{address || '请选择你的地址'}}
						</view> -->
					</u-form-item>
				</view>
				<view class="list-input">
					<u-form-item label="详细" prop="detailArea">
						<u-input v-model="userData.detailArea" placeholder="请输入详细地址" />
					</u-form-item>

				</view>
				<itmister-address-picker ref="addressEl" @confirmChange="confirmChange"></itmister-address-picker>
			</view>
		</u-form>

		<view class="tag-default">

			<view class="default-address">
				<view class="title">
					<text>默认地址</text>
				</view>
				<view class="switch-default">
					<switch class="red sm" color="#0077EE !important" checked></switch>
				</view>
			</view>
		</view>
		<view class="footer-btn">
			<view class="btn" @click="submit">
				<text>保存</text>
			</view>
		</view>
	</view>
</template>

<script>
	import itmisterAddressPicker from '@/components/itmister-address-picker/itmister-address-picker.nvue'
	export default {
		data() {
			return {
				addressType: '2',
				address: '',
				userData: {
					id: '1',
					username: '哇哈哈',
					is_chkec: '1',
					phone: '15180539372',
					addressValue: [2, 5, 23],
					detailArea: '三里团',
					values:'',
				},
				type: 'textarea',
				border: true,
				height: 100,
				autoHeight: true,


				rules: {
					username: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					intro: [{
						min: 5,
						message: '简介不能少于5个字',
						trigger: 'change'
					}],
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],

					detailArea: [{
						required: true,
						message: '请输入详情',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],

					values: [{
						required: true,
						message: '请输入收货地址',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}]
				}

			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		components: {
			itmisterAddressPicker
		},
		onLoad(params) {
			this.addressType = params.type || '2';
			uni.setNavigationBarTitle({
				title: this.addressType === '1' ? '编辑收货地址' : '新建收货地址'
			})


		},
		methods: {
			hello() {
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
					}
				});
			},

			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			},
			// 打开窗口
			selectAddress() {
				this.$refs.addressEl.show();
			},
			// 确认选中
			confirmChange(address) {
				console.log(address);
				this.address = address.province + '/' + address.city + '/' + address.area;
				this.userData.values =this.address
			},

		}

	}
</script>

<style scoped lang="scss">
	@import 'AddressEdit.scss';

	.u-form-item--left__content {
		width: 100rpx;
	}

	.uni-input-input {
		.input {
			width: 1500rpx !important;

		}

	}
</style>
